﻿#include "rankparallel.h"
#include "ui_rankparallel.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>
#include <QDate>

RankParallel::RankParallel(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RankParallel)
{
    ui->setupUi(this);
    initTableWidget();
    connect(ui->buttonSearch,SIGNAL(released()),this,SLOT(showRankData()));
}

RankParallel::~RankParallel()
{
    delete ui;
}

//初始化tableWidget
void RankParallel::initTableWidget()
{
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setStyleSheet("selection-background-color:rgb(173,216,230);"); //设置选中行的背景色
    ui->tableWidget->horizontalHeader()->resizeSection(5,100);
    ui->tableWidget->horizontalHeader()->resizeSection(6,150);
    ui->tableWidget->horizontalHeader()->resizeSection(12,100);
    ui->tableWidget->horizontalHeader()->resizeSection(13,350);
    ui->tableWidget->horizontalHeader()->resizeSection(14,120);
    ui->tableWidget->horizontalHeader()->resizeSection(16,150);

//    //年限说明
//    int rowc = ui->tableWidget_1->rowCount();//获取行数
//    for(int i=0;i <rowc;i++)
//    {
//        QDate dateNow = QDate::currentDate();
//        int y1 = dateNow.year();
//        qDebug()<<y1;
//        ui->tableWidget_1->setItem(rowc,1,new QTableWidgetItem(QString::number(y1)));
//    }
    //显示惩处代码对照表
    ui->tableWidget_2->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QSqlQuery query;
    query.exec("select PunishType,PunishPeriod from PunishTypeContrast");
    while(query.next()) //一行一行遍历
    {
        int rowc = ui->tableWidget_2->rowCount();//获取行数
        ui->tableWidget_2->insertRow(rowc);
        ui->tableWidget_2->setItem(rowc,0,new QTableWidgetItem(query.value(0).toString()));
        QDate dateNow = QDate::currentDate();
        int y2 = dateNow.year();
        ui->tableWidget_2->setItem(rowc,1,new QTableWidgetItem(QString::number(y2)));
        ui->tableWidget_2->setItem(rowc,2,new QTableWidgetItem(query.value(1).toString()));

    }
}

//职级并行计算函数
void RankParallel::showRankData()
{
    ui->tableWidget->setRowCount(0);
    QSqlQuery query;
    query.exec("select CadreName,Sex,NativePlace,IDNum,RankID,RankTime,RankSalary,UnitID from CadreInfo");
    while(query.next()) //一行一行遍历
    {
        int rowc = ui->tableWidget->rowCount();//获取行数
        ui->tableWidget->insertRow(rowc);
        //干部名称
        ui->tableWidget->setItem(rowc,0,new QTableWidgetItem(query.value(0).toString()));
        //Sex
        if(query.value(1).toInt() == 1)
            ui->tableWidget->setItem(rowc,1,new QTableWidgetItem("男"));
        if(query.value(1).toInt() == 0)
            ui->tableWidget->setItem(rowc,1,new QTableWidgetItem("女"));
        //籍贯
        QString sqlAdminArea = QString("select AdminArea from AdminAreaCode where AdminAreaID = '%1'").arg(query.value(2).toString());
        QSqlQuery queryAdminArea;
        queryAdminArea.exec(sqlAdminArea);
        while(queryAdminArea.next()) //一行一行遍历
        {
            ui->tableWidget->setItem(rowc,2,new QTableWidgetItem(queryAdminArea.value(0).toString()));
        }
        //证件号码
        ui->tableWidget->setItem(rowc,3,new QTableWidgetItem(query.value(3).toString()));
        //单位
        QString sqlUnit = QString("select unitname from unit where unitid = '%1'").arg(query.value(7).toString());
        QSqlQuery queryUnit;
        queryUnit.exec(sqlUnit);
        while(queryUnit.next()) //一行一行遍历
        {
            ui->tableWidget->setItem(rowc,4,new QTableWidgetItem(queryUnit.value(0).toString()));
        }

        //职级
        QString rankCode = query.value(4).toString();
        QString sql = QString("select RankName,YearsofPromotion from RankCode where RankID = '%1'").arg(rankCode);
        QSqlQuery rankQuery;
        rankQuery.exec(sql);
        QString rankName;
        QString yearOfPro;
        while(rankQuery.next()) //一行一行遍历
        {
            rankName = rankQuery.value(0).toString();
            yearOfPro = rankQuery.value(1).toString();
            //qDebug() << rankName;
        }
        ui->tableWidget->setItem(rowc,5,new QTableWidgetItem(rankName));
        //晋升所需年限
        ui->tableWidget->setItem(rowc,13,new QTableWidgetItem(yearOfPro));

        //级别时间
        ui->tableWidget->setItem(rowc,6,new QTableWidgetItem(query.value(5).toString()));
        //级别薪级
        ui->tableWidget->setItem(rowc,7,new QTableWidgetItem(query.value(6).toString()));

        //处理任职年限（年月）
        QDate dateNow = QDate::currentDate();
        int y1 = dateNow.year();
        int m1 = dateNow.month();
        int d1 = dateNow.day();

        QString rankYear = ui->tableWidget->item(rowc,6)->text();
        QDate dateRank = QDate::fromString(rankYear,"yyyy-MM-dd");
        int y2 = dateRank.year();
        int m2 = dateRank.month();
        int d2 = dateRank.day();

        int y = y1-y2;
        int m = m1-m2;
        int monthResult;

        if(d1>=d2)
        {
            monthResult = 12*y + m;
        }
        else
        {
            monthResult = 12*y + m -1;
        }

        QString tenure = monthToYearMonth(monthResult);
        ui->tableWidget->setItem(rowc,8,new QTableWidgetItem(tenure));

        //年限缩短or延长
//        int finalMonths = monthResult;
//        QString checkSql = QString("select CheckResult from YearCheck where CheckYear = '%1' and IDNum = '%2'").arg(y1-1).arg(query.value(3).toString());
//        QSqlQuery checkQuery;
//        checkQuery.exec(checkSql);
//        QString checkResult;
//        while(checkQuery.next()) //一行一行遍历
//        {
//            checkResult = checkQuery.value(0).toString();
//            //qDebug() << checkResult;
//        }
        int finalMonths = monthResult;
        QString checkSql = QString("select CheckResult from `Check` where IDNum = '%1'").arg(query.value(3).toString());
        QSqlQuery checkQuery;
        checkQuery.exec(checkSql);
        QString checkResult;
        int perfect=0;
        int bad=0;
        while(checkQuery.next()) //一行一行遍历
        {
            checkResult = checkQuery.value(0).toString();
            //qDebug() << checkResult;

            if(checkResult == "优秀")
            {
                perfect++;
                finalMonths = finalMonths + 6;
                //qDebug()<<finalMonths;
            }
            if(checkResult == "基本称职" or checkResult == "不定等次" )
            {
                bad++;
                finalMonths = finalMonths-12;
                //qDebug()<<finalMonths;
            }
        }
        //年限缩短
        perfect = perfect * 6;
        QString yearShort = monthToYearMonth(perfect);
        ui->tableWidget->setItem(rowc,9,new QTableWidgetItem(yearShort));
        //年限延长
        bad = bad * 12;
        QString yearLong = monthToYearMonth(bad);
        ui->tableWidget->setItem(rowc,10,new QTableWidgetItem(yearLong));
        //年限结果
        QString finalYearText = monthToYearMonth(finalMonths);
        ui->tableWidget->setItem(rowc,11,new QTableWidgetItem(finalYearText));

//        //处理任职年限（总天数）
//        QDate dateNow = QDate::currentDate();
//        QString rankYear = ui->tableWidget->item(rowc,5)->text();
//        qDebug()<< rankYear;
//        QDate dateRank = QDate::fromString(rankYear,"yyyy-MM-dd");
//        qDebug()<< dateRank;
//        qDebug()<<dateRank.year();
//        qDebug()<<dateRank.month();
//        int days = dateRank.daysTo(dateNow); //相差天数
//        //qDebug()<<days;

//        //对天数进行处理->x年x月
//        QString yearText = yearMonth(days);
//        ui->tableWidget->setItem(rowc,7,new QTableWidgetItem(yearText));

//        //年限缩短or延长
//        int finalDays = days;
//        QString checkSql = QString("select CheckResult from YearCheck where CheckYear = '2019' and IDNum = '%1'").arg(query.value(3).toString());
//        QSqlQuery checkQuery;
//        checkQuery.exec(checkSql);
//        QString checkResult;
//        while(checkQuery.next()) //一行一行遍历
//        {
//            checkResult = checkQuery.value(0).toString();
//            //qDebug() << checkResult;
//        }
//        if(checkResult == "优秀")
//        {
//            ui->tableWidget->setItem(rowc,8,new QTableWidgetItem("6月"));
//            finalDays=days+6*30;
//            //qDebug()<<finalDays;
//        }
//        if(checkResult == "不定等次")
//        {
//            ui->tableWidget->setItem(rowc,9,new QTableWidgetItem("1年"));
//            finalDays=days-12*30;
//            //qDebug()<<finalDays;
//        }

//        //年限结果
//        QString finalYearText = yearMonth(finalDays);
//        ui->tableWidget->setItem(rowc,10,new QTableWidgetItem(finalYearText));

        //晋升职级
        QString idSql = QString("select ID from RankCode where RankID = '%1'").arg(rankCode);
        QSqlQuery idQuery;
        idQuery.exec(idSql);

        int id;
        while(idQuery.next()) //一行一行遍历
        {
            id = idQuery.value(0).toInt();
            //qDebug() << id;
        }

        if(id>1)
        {
            QString upRankSql = QString("select RankName from RankCode where ID = '%1'").arg(id-1);
            QSqlQuery upRankQuery;
            upRankQuery.exec(upRankSql);

            QString upRankName;
            while(upRankQuery.next()) //一行一行遍历
            {
                upRankName = upRankQuery.value(0).toString();
                //qDebug() << upRankName;
            }
            ui->tableWidget->setItem(rowc,12,new QTableWidgetItem(upRankName));

            //晋升职级薪资
            QString upSalarySql = QString("select CorrespondingLevel from RankCode where ID = '%1'").arg(id-1);
            QSqlQuery upSalaryQuery;
            upSalaryQuery.exec(upSalarySql);

            QString upSalary;
            while(upSalaryQuery.next()) //一行一行遍历
            {
                upSalary = upSalaryQuery.value(0).toString();
                //qDebug() << upSalaryName;
            }
            ui->tableWidget->setItem(rowc,14,new QTableWidgetItem(upSalary));

        }

        //处分期内
        QString punishSql = QString("select PunishTime,PunishTypeID from Punish where Punish.IDNum ='%1'").arg(query.value(3).toString());
        QSqlQuery punishQuery;
        punishQuery.exec(punishSql);

        QString punishTime;
        int punishType;
        while(punishQuery.next()) //一行一行遍历
        {
            punishTime = punishQuery.value(0).toString();
            //qDebug() << "1"<<punishTime;
            ui->tableWidget->setItem(rowc,15,new QTableWidgetItem(punishTime));
            punishType = punishQuery.value(1).toInt();
            //qDebug() << punishType;

            QString punishPeriodSql = QString("select PunishPeriod from PunishTypeContrast where PunishTypeID ='%1'").arg(punishType);
            QSqlQuery punishPeriodQuery;
            punishPeriodQuery.exec(punishPeriodSql);
            int punishPeriodResult;
            while(punishPeriodQuery.next()) //一行一行遍历
            {
                punishPeriodResult = punishPeriodQuery.value(0).toInt();
                //qDebug() << punishPeriodQuery.value(0).toInt();
                //qDebug() << punishPeriodQuery.value(1).toInt();
            }

            QDate dateNow = QDate::currentDate();
            int y1 = dateNow.year();
            int m1 = dateNow.month();
            int d1 = dateNow.day();

            QDate pTime = QDate::fromString(punishTime,"yyyy-MM-dd");
            int y2 = pTime.year();
            int m2 = pTime.month();
            int d2 = pTime.day();

            int y = y1-y2;
            int m = m1-m2;
            int monthResult;

            if(d1>=d2)
            {
                monthResult = 12*y + m;
            }
            else
            {
                monthResult = 12*y + m -1;
            }

            if(monthResult >= punishPeriodResult)
            {
                ui->tableWidget->setItem(rowc,15,new QTableWidgetItem("否"));
            }
            else
            {
                ui->tableWidget->setItem(rowc,15,new QTableWidgetItem("是"));
            }

//            //粗略判断是否处分期内
//            QDate dateNow = QDate::currentDate();
//            //qDebug()<< rankYear;
//            QDate pTime = QDate::fromString(punishTime,"yyyy-MM-dd");
//            //qDebug()<< dateRank;
//            int diffdays = pTime.daysTo(dateNow); //相差天数
//            //qDebug()<<diffdays;

//            if(diffdays > punishPeriodResult*30)
//            {
//                ui->tableWidget->setItem(rowc,14,new QTableWidgetItem("否"));
//            }
//            else
//            {
//                ui->tableWidget->setItem(rowc,14,new QTableWidgetItem("是"));
//            }

        }

        //晋升职级进度
        if(ui->tableWidget->item(rowc,12))
        {
            ui->tableWidget->setItem(rowc,16,new QTableWidgetItem("请关注年限结果"));
        }
        else
        {
            ui->tableWidget->setItem(rowc,16,new QTableWidgetItem("不在实施范围"));
        }


        int totalRows = ui->tableWidget->rowCount();
        QString newtext = QString(" 共: %1 条").arg(totalRows);
        ui->label_2->setText(newtext);
     }
}

QString RankParallel::monthToYearMonth(int &months)
{
    QString yearText;
    if(months/12 > 0)
    {
        if(months % 12 != 0)
        {
            yearText = QString("%1年%2月").arg(months / 12).arg(months % 12);
        }
        else
        {
            yearText = QString("%1年").arg(months / 12);
        }

    }
    else
    {
        yearText = QString("%1月").arg(months);
    }
    return yearText;
}

//查找某个干部信息
void RankParallel::on_buttonFind_clicked()
{
    int rows = ui->tableWidget->rowCount();
    QString gbName = ui->lineEdit->text();
    if ("" == gbName)
    {   //判断输入是否为空
        for (int i = 0; i < rows; i++)
        {
            ui->tableWidget->setRowHidden(i, false);//显示所有行
            int rowc = ui->tableWidget->rowCount();
            QString newtext = QString(" 共: %1 条").arg(rowc);
            ui->label_2->setText(newtext);
        }
    }
    else
    {
        QList <QTableWidgetItem *> item = ui->tableWidget->findItems(gbName, Qt::MatchContains);

        for (int i = 0; i < rows; i++)
        {
            ui->tableWidget->setRowHidden(i, true);//隐藏所有行
        }

        if (!item.isEmpty())
        {
            for (int i = 0; i < item.count(); i++)
            {
                ui->tableWidget->setRowHidden(item.at(i)->row(),false);//item.at(i).row()输出行号
            }

            int totalRows = item.count();
            QString newtext = QString(" 共: %1 条").arg(totalRows);
            ui->label_2->setText(newtext);

        }
    }
}

//QString RankParallel::yearMonth(int &days)
//{
//    //对天数进行处理->x年x月
//    int year,month;
//    QString yearText;
//    //int day = 28;
//    year = days / 365;
//    //qDebug()<< year;
//    month = (days % 365)/30;
//    //qDebug()<< month;
//    if(year > 0)
//    {
//        yearText = QString("%1年%2月").arg(year).arg(month);
//    }
//    else
//    {
//        yearText = QString("%1月").arg(month);
//    }
//    return yearText;
//}

void RankParallel::on_buttonSearch_clicked()
{

}
